/**
 * <P>用户登录</P>
 * @author ztjie
 * */
$(function() {
	var requestType = "login";
	$(".message_box").hide();
	$('#login_mobile').blur(function() {
		return user.verifyMobile();
	});
	var mobilePhone = SystemUtil.getUrlParam("mobile_phone");
	if(!SystemUtil.isNull(mobilePhone)){
		$("#login_mobile").attr({
			value: mobilePhone,
			readonly: true
		});
		requestType = "regist";
		user.loginCodeTime($(".iphone_time"), 59);
	}else{
		$(".iphone_time").hide();
	}
	//登录页面的事件添加
	$(".iphone_cf").on("click",function(){
		user.loginCodeTime($(".iphone_time"), 60);
	});
	$(".inphone_sublit").on("click",function(){
		if(requestType == "regist"){
			user.request("userAccount/regist");
		}else{
			user.request("userAccount/login");	
		}
	});
});
var user = {
	loginCodeTime : function(o, wait) {
		var mobilePhone = $("#login_mobile").val();
		user.verifyMobile();
		if (wait == 60) {
			var params = {
				"mobilePhone" : mobilePhone,
				"verifyCodeType" : 1
			};
			user.userAjax(params, "verify/sendVerifyCode", "verifyCode");
		}
		if (wait == 0) {
			$(".iphone_cf").show();
			$(o).hide();
		} else {
			$(".iphone_cf").hide();
			$(o).show();
			$(o).html(wait + "秒后重新获取");
			wait--;
			setTimeout(function() {
				user.loginCodeTime(o, wait)
			}, 1000)
		}
	},
	userAjax : function(request, URL, type) {
		if (!SystemUtil.isNull(request)){
			$.ajax({
				type : "POST",
				url : SystemConfig.baseServiceUrl + URL,
				data : $.toJSON(request),
				contentType : "application/json; charset=UTF-8",
				dataType : "json",
				success : function(resp) {
					if(type!="verifyCode"){
						var redirectUri = SystemUtil.getUrlParam("Redirect_Uri");
						if(SystemUtil.isNull(redirectUri)){
							redirectUri=SystemConfig.baseHtmlUrl+"index.html";
						}
						window.location.href = redirectUri;
					}
				},
				exception : function(resp) {
					var message = resp.responseStatus.message;
					alert(message);
				}
			});
		}
	},
	verifyMobile : function() {
		var mobileVlue = $('#login_mobile').val();
		if (!Validation.isNotEmpty(mobileVlue)) {
			$(".message_box").show();
			$("#mobileInfo_span").html("手机号不能为空");
			return false;
		}
		if (!Validation.isMobileNum(mobileVlue)) {
			$(".message_box").show();
			$("#mobileInfo_span").html("手机号码格式错误");
			return false;
		} else {
			$(".message_box").hide();
			$("#mobileInfo_span").html("");
			return true;
		}
	},
	request : function(uri) {
		if(user.verifyMobile()){
			var t = {
				"mobilePhone": $("#login_mobile").val(),
				"verifyCode": $("#verify_code").val()
			};
			user.userAjax(t, uri);
		}
	}
}